package com.atsmart.log;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.atsmart.app.BizApp;

import android.os.Environment;

public class UserLog {
	// 日志文件保存的路径以及日志文件名
	String fileSucess = "/inspection_sucess.txt";// 巡检成功log
	String fileException = "/inspection_excption.txt";// 巡检异常log
	String fileNoWeb = "/inspection_unintent.txt";// 无网络log
	String fileFromSql = "/inspection_fromsql.txt";//保存本地上传
	String sqlExceptioin = "/inspection_sqlexception.txt";//保存本地上传异常
	String filePosition = "/inspection_startPosition.txt";//巡检条件
	String apkinfo = "/apk_infos.txt";//监听apk安装、替换、卸载
	private static UserLog instance;

	public synchronized static UserLog getInstance() {
		if (instance == null) {
			instance = new UserLog();
		}
		return instance;
	}

	public void createSucessFile() {
		// 判断sd卡是否存在
		if (Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {
			String filePath = BizApp.getInstance().getFilePath() + "/inspection";
			File file = new File(filePath);
			if(!file.exists()){
				file.mkdirs();
			}
			File dir = new File(filePath + fileSucess);
			// 如果文件不存在则创建新文件
			if (!dir.exists()) {
				try {
					dir.createNewFile();
				} catch (IOException e) {
				}
			}
		}
	}
	
	public void createApkInfo() {
		// 判断sd卡是否存在
		if (Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {
			String filePath = BizApp.getInstance().getFilePath() + "/inspection";
			File file = new File(filePath);
			if(!file.exists()){
				file.mkdirs();
			}
			File dir = new File(filePath + apkinfo);
			// 如果文件不存在则创建新文件
			if (!dir.exists()) {
				try {
					dir.createNewFile();
				} catch (IOException e) {
				}
			}
		}
	}
	
	public void writeApkInfo(String content) {
		try {
			if (Environment.getExternalStorageState().equals(
					Environment.MEDIA_MOUNTED)) {
				String filePath = BizApp.getInstance().getFilePath() + "/inspection";
				File targetFile = new File(filePath + apkinfo);
				// 以指定文件创建RandomAccessFile对象
				RandomAccessFile accessFile = new RandomAccessFile(targetFile,
						"rw");
				// 创建时间对象
				SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"
						+ " " + "hh:mm:ss");
				// 将文件记录指针移到最后
				accessFile.seek(targetFile.length());
				// 输出日志信息到文件中
				accessFile
						.write(("\n" + dateFormat.format(new Date()).toString()
								+ " : " + content).getBytes());
				// 关闭文件
				accessFile.close();
			}
		} catch (Exception e) {
		}
	}

	public void writeSucessFile(String content) {
		try {
			if (Environment.getExternalStorageState().equals(
					Environment.MEDIA_MOUNTED)) {
				String filePath = BizApp.getInstance().getFilePath() + "/inspection";
				File targetFile = new File(filePath + fileSucess);
				// 以指定文件创建RandomAccessFile对象
				RandomAccessFile accessFile = new RandomAccessFile(targetFile,
						"rw");
				// 创建时间对象
				SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"
						+ " " + "hh:mm:ss");
				// 将文件记录指针移到最后
				accessFile.seek(targetFile.length());
				// 输出日志信息到文件中
				accessFile
						.write(("\n" + dateFormat.format(new Date()).toString()
								+ " : " + content).getBytes());
				// 关闭文件
				accessFile.close();
			}
		} catch (Exception e) {
		}
	}
	
	public void createFilePosition() {
		// 判断sd卡是否存在
		if (Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {
			String filePath = BizApp.getInstance().getFilePath() + "/inspection";
			File file = new File(filePath);
			if(!file.exists()){
				file.mkdirs();
			}
			File dir = new File(filePath + filePosition);
			// 如果文件不存在则创建新文件
			if (!dir.exists()) {
				try {
					dir.createNewFile();
				} catch (IOException e) {
				}
			}
		}
	}
	
	public void writeFilePosition(String content) {
		try {
			if (Environment.getExternalStorageState().equals(
					Environment.MEDIA_MOUNTED)) {
				String filePath = BizApp.getInstance().getFilePath() + "/inspection";
				File targetFile = new File(filePath + filePosition);
				// 以指定文件创建RandomAccessFile对象
				RandomAccessFile accessFile = new RandomAccessFile(targetFile,
						"rw");
				// 创建时间对象
				SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"
						+ " " + "hh:mm:ss");
				// 将文件记录指针移到最后
				accessFile.seek(targetFile.length());
				// 输出日志信息到文件中
				accessFile
						.write(("\n" + dateFormat.format(new Date()).toString()
								+ " : " + content).getBytes());
				// 关闭文件
				accessFile.close();
			}
		} catch (Exception e) {
		}
	}
	public void createSqlException() {
		// 判断sd卡是否存在
		if (Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {
			String filePath = BizApp.getInstance().getFilePath() + "/inspection";
			File file = new File(filePath);
			if(!file.exists()){
				file.mkdirs();
			}
			File dir = new File(filePath + sqlExceptioin);
			// 如果文件不存在则创建新文件
			if (!dir.exists()) {
				try {
					dir.createNewFile();
				} catch (IOException e) {
				}
			}
		}
	}
	
	public void writeSqlException(String content) {
		try {
			if (Environment.getExternalStorageState().equals(
					Environment.MEDIA_MOUNTED)) {
				String filePath = BizApp.getInstance().getFilePath() + "/inspection";
				File targetFile = new File(filePath + sqlExceptioin);
				// 以指定文件创建RandomAccessFile对象
				RandomAccessFile accessFile = new RandomAccessFile(targetFile,
						"rw");
				// 创建时间对象
				SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"
						+ " " + "hh:mm:ss");
				// 将文件记录指针移到最后
				accessFile.seek(targetFile.length());
				// 输出日志信息到文件中
				accessFile
						.write(("\n" + dateFormat.format(new Date()).toString()
								+ " : " + content).getBytes());
				// 关闭文件
				accessFile.close();
			}
		} catch (Exception e) {
		}
	}
	public void createException() {
		// 判断sd卡是否存在
		if (Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {
			String filePath = BizApp.getInstance().getFilePath() + "/inspection";
			File file = new File(filePath);
			if(!file.exists()){
				file.mkdirs();
			}
			File dir = new File(filePath + fileException);
			// 如果文件不存在则创建新文件
			if (!dir.exists()) {
				try {
					dir.createNewFile();
				} catch (IOException e) {
				}
			}
		}
	}

	public void writeException(String content) {
		try {
			if (Environment.getExternalStorageState().equals(
					Environment.MEDIA_MOUNTED)) {
				String filePath = BizApp.getInstance().getFilePath() + "/inspection";
				File targetFile = new File(filePath + fileException);
				// 以指定文件创建RandomAccessFile对象
				RandomAccessFile accessFile = new RandomAccessFile(targetFile,
						"rw");
				// 创建时间对象
				SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"
						+ " " + "hh:mm:ss");
				// 将文件记录指针移到最后
				accessFile.seek(targetFile.length());
				// 输出日志信息到文件中
				accessFile
						.write(("\n" + dateFormat.format(new Date()).toString()
								+ " : " + content).getBytes());
				// 关闭文件
				accessFile.close();
			}
		} catch (Exception e) {
		}
	}
	 
	public void createUnIntent() {
		// 判断sd卡是否存在
		if (Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {
			String filePath = BizApp.getInstance().getFilePath() + "/inspection";
			File file = new File(filePath);
			if(!file.exists()){
				file.mkdirs();
			}
			File dir = new File(filePath + fileNoWeb);
			// 如果文件不存在则创建新文件
			if (!dir.exists()) {
				try {
					dir.createNewFile();
				} catch (IOException e) {
				}
			}
		}
	}

	public void writeUnIntent(String content) {
		try {
			if (Environment.getExternalStorageState().equals(
					Environment.MEDIA_MOUNTED)) {
				String filePath = BizApp.getInstance().getFilePath() + "/inspection";
				File targetFile = new File(filePath + fileNoWeb);
				// 以指定文件创建RandomAccessFile对象
				RandomAccessFile accessFile = new RandomAccessFile(targetFile,
						"rw");
				// 创建时间对象
				SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"
						+ " " + "hh:mm:ss");
				// 将文件记录指针移到最后
				accessFile.seek(targetFile.length());
				// 输出日志信息到文件中
				accessFile
						.write(("\n" + dateFormat.format(new Date()).toString()
								+ " : " + content).getBytes());
				// 关闭文件
				accessFile.close();
			}
		} catch (Exception e) {
		}
	}
	
	
	public void createSqlFile() {
		// 判断sd卡是否存在
		if (Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {
			String filePath = BizApp.getInstance().getFilePath() + "/inspection";
			File file = new File(filePath);
			if(!file.exists()){
				file.mkdirs();
			}
			File dir = new File(filePath + fileFromSql);
			// 如果文件不存在则创建新文件
			if (!dir.exists()) {
				try {
					dir.createNewFile();
				} catch (IOException e) {
				}
			}
		}
	}

	public void writeSqlFile(String content) {
		try {
			if (Environment.getExternalStorageState().equals(
					Environment.MEDIA_MOUNTED)) {
				String filePath = BizApp.getInstance().getFilePath() + "/inspection";
				File targetFile = new File(filePath + fileFromSql);
				// 以指定文件创建RandomAccessFile对象
				RandomAccessFile accessFile = new RandomAccessFile(targetFile,
						"rw");
				// 创建时间对象
				SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"
						+ " " + "hh:mm:ss");
				// 将文件记录指针移到最后
				accessFile.seek(targetFile.length());
				// 输出日志信息到文件中
				accessFile
						.write(("\n" + dateFormat.format(new Date()).toString()
								+ " : " + content).getBytes());
				// 关闭文件
				accessFile.close();
			}
		} catch (Exception e) {
		}
	}
}
